﻿using System;
using System.Windows;
using Microsoft.Practices.Composite.Modularity;
using Microsoft.Practices.Composite.UnityExtensions;
using TelecoSystems.Infrastructure.Views;
using TelecoSystems.ViewModels;
using TelecoSystems.Views;

namespace TelecoSystems
{
    public partial class Bootstrapper : UnityBootstrapper
    {
        private App _app;
        
        public Bootstrapper(App app)
        {
            _app = app;

            if (_app.IsRunningOutOfBrowser) {
                _app.CheckAndDownloadUpdateCompleted += 
                    new CheckAndDownloadUpdateCompletedEventHandler(_app_CheckAndDownloadUpdateCompleted);
                _app.CheckAndDownloadUpdateAsync();
            }
        }

        void _app_CheckAndDownloadUpdateCompleted(object sender, CheckAndDownloadUpdateCompletedEventArgs e) {
            if (e.Error != null) {
                ErrorWindow.CreateNew(e.Error);
            }
            else if (e.UpdateAvailable) {
                InformationWindow.Create("Application was successfully updated, please restart it.", "Application updated");
            }
        }

        protected override void ConfigureContainer()
        {                                 
            base.ConfigureContainer();
        }

        protected override IModuleCatalog GetModuleCatalog()
        {            
            return ModuleCatalog.CreateFromXaml(
                        new Uri("/TelecoSystems;component/ModuleCatalog.xaml",
                        UriKind.Relative));            
        }

        protected override DependencyObject CreateShell()
        {
            ShellViewModel viewModel = Container.Resolve<ShellViewModel>();
            ShellViewModel.ModuleCatalog = GetModuleCatalog();            
            
            // Use the container to create an instance of the shell.
            ShellView view = Container.Resolve<ShellView>();
            //LoginContainer view = Container.Resolve<LoginContainer>();

            // Set it as the root visual for the application.
            //Application.Current.RootVisual = view;
            _app.progressIndicator.Content = view;

            return view;
        }
    }
}
